<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Auctions\CollectPm;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Admin\Extensions\Tools\InStorage;
class CollectPmController extends Controller
{
    protected $name = '拍卖采集管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
	//入库
	public function InStorage()
	{
		$data = request()->all();
		if(empty($data['id']))return ['status'=>0, 'message'=> '请勾选要入库的拍卖.'];
		
		$ids = explodeId($data['id']);
		
		$list = CollectPm::whereIn('id', $ids)->get();
		
		foreach($list as $row)
		{
			
			$row->status = 2;
			$row->save();
		}
		return ['status'=>1, 'message'=> '入库成功！请耐心等待'];
	}
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        // foreach (CollectPm::all() as $key => $value) {
        //     # code...
        //     $value->company_title = $value->company['title'];
        //     $value->save();
        // }
        return Admin::grid(CollectPm::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->tools(function ($tools) {
				$tools->append(new InStorage());
			});
            $grid->filter(function($filter){
                
                $filter->like('title', '标题');
                $filter->like('company_title', '公司名称');
                //$filter->addFilter($filter->like('post_title', '标题'));
                $filter->equal('is_show','显示')->select(CollectPm::$status);
            
            });
            $grid->column('id', 'ID')->sortable();
            
            $grid->column('title', '名称');
            $grid->column('prev_time', '预展时间');
            $grid->column('company', '公司名称')->display(function($val){
				return $val['title'];
			});
            $grid->column('status', '状态')->display(function($val){
				return CollectPm::$status[$val];
			});
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(CollectPm::class, function (Form $form) {
            
            
			$form->saving(function(Form $form){
				// $form->ignore(['content']);
				
				
				
				// $form->ignore(['content']);
				// dd($form->content);
				// if($form->content['thumb'] == '')
				// {
					// $form->content['thumb'] = $form->model()->content['thumb'];
				// }
				// dd($form->content);
			});
			
            
        });
    }
}
